﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'InsertDatosProducto')
	BEGIN
		DROP  Procedure  InsertDatosProducto
	END

GO

CREATE Procedure InsertDatosProducto

	(
		   @stockActual decimal(8,4),
	       @precio money,
           @IdBien int
	)


AS
BEGIN
SET NOCOUNT ON
	
	BEGIN TRY
UPDATE [dbo].[Bien] 
			SET [stockActual] = [stockActual] + @stockActual,[Precio]=@precio
			WHERE [IdBien]=@IdBien
DECLARE @stock decimal(8,4)
	SET @stock = ISNULL((SELECT StockActual FROM Bien WHERE IdBien = @idBien), 0)
	
	IF @stock < 1 
		BEGIN
			UPDATE Bien SET Estado = 0 WHERE IdBien = @idBien
		END
	ELSE
		BEGIN
			UPDATE Bien SET Estado = 1 WHERE IdBien = @idBien
		END 
	END TRY

    BEGIN CATCH
		EXEC RethrowError;
	END CATCH
    
    SET NOCOUNT OFF		
End

GO


GRANT EXEC ON InsertDatosProducto TO PUBLIC

GO
